<?php
/**
 * Created by PhpStorm.
 * Date: 2018/11/29
 * Time: 下午2:22
 */

namespace app\api\service\payment\F4stpay;

class Client
{
    private $mercId;
    private $mercKey;
    const API = "http://api.wenzipay.cn/open/v1";

    public function __construct($mercId, $mercKey)
    {
        $this->mercId = $mercId;
        $this->mercKey = $mercKey;
    }

    /**
     * 创建支付订单，返回支付二维码
     * @param $orderData
     * @return array
     * @throws F4stPayException
     */
    public function createPayOrder($orderData)
    {
        // 必须包含如下字段
        $requiredKeys = array("mercOrderId", "amount", "channel", "productName", "notifyUrl");
        $this->__checkRequireField($requiredKeys, $orderData);
        $this->__addDefaultParam($orderData);
        $res = HTTP::post(self::API . "/pay/order", $orderData);
        return json_decode($res["body"], true);
    }

    /**
     * 查询支付订单信息
     * @param $mercOrderId
     * @return array
     * @throws Exceptions\F4stPayException
     */
    public function queryPayOrder($mercOrderId)
    {
        $params = array(
            "mercOrderId" => $mercOrderId
        );
        $this->__addDefaultParam($params);
        $res = HTTP::get(self::API . "/pay/order", $params);
        return json_decode($res["body"], true);
    }

    /**
     * 创建代付订单
     * @param $orderData
     * @return array
     * @throws F4stPayException
     */
    public function createWithdrawOrder($orderData)
    {
        // 必须包含如下字段
        $requiredKeys = array("mercOrderId", "amount", "bankAccountNo", "bankAccountName", "bankName", "bankNameSub",
            "bankId", "bankAccountIdNo", "bankAccountMobile");
        $this->__checkRequireField($requiredKeys, $orderData);
        $this->__addDefaultParam($orderData);
        $res = HTTP::post(self::API . "/withdraw/order", $orderData);
        return json_decode($res["body"], true);
    }

    /**
     * 查询代付订单信息
     * @param $mercOrderId
     * @return array
     * @throws Exceptions\F4stPayException
     */
    public function queryWithdrawOrder($mercOrderId)
    {
        $params = array(
            "mercOrderId" => $mercOrderId
        );
        $this->__addDefaultParam($params);
        $res = HTTP::get(self::API . "/withdraw/order", $params);
        return json_decode($res["body"], true);
    }

    /**
     * 获取商户信息，包含余额，额度限制等
     * @throws Exceptions\F4stPayHTTPException
     */
    public function getMercInfo()
    {
        $params = array();
        $this->__addDefaultParam($params);
        $res = HTTP::get(self::API . "/merc", $params);
        return json_decode($res["body"], true);
    }

    /**
     *
     * @param $requiredKeys
     * @param $params
     * @throws F4stPayException
     */
    private function __checkRequireField($requiredKeys, $params)
    {
        foreach ($requiredKeys as $key) {
            if (!key_exists($key, $params)) {
                throw new F4stPayException("missing required field: {$key}");
            }
        }
    }

    /**
     * 创建支付订单，返回支付二维码
     * @param array $data
     */
    private function __addDefaultParam(&$data)
    {
        $data["mercId"] = $this->mercId;
        $data["time"] = time();
        $data["sign"] = $this->__sign($data);
    }

    /**
     * 签名
     * @param $data array
     * @return string
     */
    private function __sign($data)
    {
        ksort($data);
        $dStr = http_build_query($data);
        return md5($dStr . $this->mercKey);
    }
}